using BackData;
using Domain.DTOs.RBAC;
using Domain.RBAC;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.Users.Permissions;

namespace Team.API.Applocation.Handler.Users.Permissions
{
    /// <summary>
    /// 更新权限处理器
    /// </summary>
    public class UpdatePermissionHandler : IRequestHandler<UpdatePermissionCommand, APIResult<PermissionDTO>>
    {
        private readonly IRepository<Permissionmodel> permissionRepository;

        public UpdatePermissionHandler(IRepository<Permissionmodel> permissionRepository)
        {
            this.permissionRepository = permissionRepository;
        }

        public async Task<APIResult<PermissionDTO>> Handle(UpdatePermissionCommand request, CancellationToken cancellationToken)
        {
            // 查询权限是否存在
            var permission = await permissionRepository.GetByIdAsync(request.Id);
            if (permission == null || permission.IsDel)
            {
                return APIResult<PermissionDTO>.Faile("权限不存在");
            }

            // 更新权限信息（只更新非空字段）
            if (!string.IsNullOrWhiteSpace(request.PermissionName))
            {
                permission.PermissionName = request.PermissionName.Trim();
            }

            if (request.PermissionUrl != null)
            {
                permission.PermissionUrl = request.PermissionUrl;
            }

            if (request.PermissionType.HasValue)
            {
                permission.PermissionType = request.PermissionType.Value;
            }

            if (request.Icon != null)
            {
                permission.Icon = request.Icon;
            }

            if (request.Sort.HasValue)
            {
                permission.Sort = request.Sort.Value;
            }

            if (request.Status.HasValue)
            {
                permission.Status = request.Status.Value;
            }

            if (request.Description != null)
            {
                permission.Description = request.Description;
            }

            if (request.Remark != null)
            {
                permission.Remark = request.Remark;
            }

            // 更新审计信息
            //permission.Updatedate = DateTime.Now;
            //permission.Updatename = "系统管理员"; // TODO: 从当前登录用户获取

            int rows = await permissionRepository.UpdateAsync(permission);
            if (rows <= 0)
            {
                return APIResult<PermissionDTO>.Faile("更新权限失败");
            }

            // 构造返回DTO
            var dto = new PermissionDTO
            {
                Id = permission.Id,
                PermissionCode = permission.PermissionCode,
                PermissionName = permission.PermissionName,
                PermissionUrl = permission.PermissionUrl,
                ParentId = permission.ParentId,
                PermissionType = permission.PermissionType,
                Icon = permission.Icon,
                Sort = permission.Sort,
                Status = permission.Status,
                Description = permission.Description,
                Remark = permission.Remark
            };

            return APIResult<PermissionDTO>.Ok("更新权限成功", dto);
        }
    }
}
